package com.jawmask.android.discover.bean.task;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.jawmask.android.discover.bean.GPSLocation;
import com.jawmask.android.discover.bean.SearchRequest;
import com.jawmask.android.discover.bean.SearchResult;
import com.jawmask.android.discover.utils.AppConstants.GOOGLE_SEARCH_PLACE_STATUS;
import com.jawmask.android.discover.utils.AppConstants.HTTP_URL;
import com.jawmask.android.discover.utils.UserSettings;

public class PlaceDetailTask extends BaseDiscoverTask<SearchRequest, Void, SearchResult> {

	public PlaceDetailTask(final Context context) {
		super(context);
	}

	@Override
	protected URI getRequestURI(SearchRequest... searchRequests) throws URISyntaxException {
		URI requestUrl = URI.create(HTTP_URL.GOOGLE_SEARCH_PLACE_DETAIL_API_URL.httpUrlValue);
		SearchRequest searchRequest = searchRequests[0];

		UserSettings userSettings = UserSettings.getInstance();

		List<NameValuePair> requestParameter = new ArrayList<NameValuePair>();
		requestParameter.add(new BasicNameValuePair("key", getGoogleAppKey()));
		requestParameter.add(new BasicNameValuePair("reference", searchRequest.getReferenceNumber()));
		requestParameter.add(new BasicNameValuePair("sensor", String.valueOf(searchRequest.isLocationFromDevice())));

		requestParameter.add(new BasicNameValuePair("language", userSettings.getUserSelectedLanguage()));

		return URIUtils.createURI(requestUrl.getScheme(), requestUrl.getAuthority(), -1, requestUrl.getPath(), URLEncodedUtils.format(requestParameter, HTTP.UTF_8), null);
	}

	protected SearchResult getObjectFromJSON(JSONObject searchResultsJson) throws JSONException {
		String status = searchResultsJson.getString("status");

		if (status.equalsIgnoreCase(GOOGLE_SEARCH_PLACE_STATUS.OK.status)) {
			JSONObject searchResultJson = searchResultsJson.getJSONObject("result");

			SearchResult searchResult = new SearchResult();

			JSONObject geometryJsonObject = searchResultJson.getJSONObject("geometry");
			JSONObject locationJsonObject = geometryJsonObject.getJSONObject("location");

			GPSLocation gpsLocation = new GPSLocation();
			gpsLocation.setLatitude(locationJsonObject.optDouble("lat"));
			gpsLocation.setLongitude(locationJsonObject.optDouble("lng"));

			searchResult.setIconPath(searchResultJson.optString("icon"));
			searchResult.setResultId(searchResultJson.optString("id"));
			searchResult.setName(searchResultJson.optString("name"));
			searchResult.setAddress(searchResultJson.optString("vicinity"));
			searchResult.setReference(searchResultJson.optString("reference"));

			searchResult.setInternationalPhoneNumber(searchResultJson.optString("international_phone_number"));
			searchResult.setMapUrl(searchResultJson.optString("url"));
			searchResult.setWebsite(searchResultJson.optString("website"));
			searchResult.setFullAddress(searchResultJson.optString("formatted_address"));

			return searchResult;
		}
		return null;
	}
}
